import { reactive, toRaw } from 'vue';
// 分页钩子函数
interface Options {
	tips?: number | string;
	seconds?: number | string;
	mobile?: number;
	// fetchFun : (_arg : any) => Promise<any>
}
export function useCodes(options: Options) {
	const code_info = reactive({
		tips: '获取验证码',
		seconds: 60,
		mobile: '',
		times: 60
	});

	const sentCode = () => {
		if (code_info.tips === '获取验证码') {
			if (uni.$u.test.mobile(code_info.mobile)) {
				// 模拟向后端请求验证码
				// uni.$u.http.post('/v1.0/sms/send', {
				// 	type: '1',
				// 	mobile: code_info.mobile,
				// }).then(() => {
				code_info.tips = code_info.seconds + 's';
				let clear = setInterval(() => {
					code_info.seconds -= 1;
					code_info.tips = code_info.seconds + 's';
					if (code_info.seconds == 0) {
						clearInterval(clear);
						code_info.tips = '获取验证码';
						code_info.seconds = code_info.times;
					}
				}, 1000);
				// })
			} else {
				uni.$u.toast('请输入正确手机号');
			}
		} else {
			uni.$u.toast('倒计时结束后再发送');
		}
	};
	const verifyCode = () => {};

	return {
		code_info,
		sentCode,
		verifyCode
	};
}
